package org.business.accounting.components;

import java.util.Locale;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.PersistentLocale;
import org.business.accounting.beans.UserBean;
import org.business.accounting.interfaces.LogOutHelper;
import org.business.accounting.interfaces.User;
import org.business.accounting.pages.Dealer;
import org.business.accounting.pages.Index;
import org.business.accounting.pages.LogIn;
import org.business.accounting.pages.Preview;
import org.business.accounting.pages.Purchase;
import org.business.accounting.pages.Registration;
import org.business.accounting.pages.ShowCart;
import org.business.accounting.pages.ShowUsers;
import org.business.accounting.pages.UserDetails;

@SuppressWarnings("unused")
public class Layout {
	
	@Parameter
	@Property
	private User user;
	
	@Parameter(required = true)
	@Property
	private boolean userExists;
	
	@Parameter(required = true)
	@Property
	private boolean isAdmin;
	
	@Parameter(required = true)
	@Property
	private LogOutHelper logOutHelper;
	
	//
	// Internationalization
	//
	
	@org.apache.tapestry5.annotations.Component
	private LanguageSwitcher languageSwitcher;
	
	//
	// LogIn
	//
	
	@Property
	private User tmpUser;
	
	@org.apache.tapestry5.annotations.Component(parameters = {
			"currentUser = user",
			"tmpUser = tmpUser"})
	private org.business.accounting.components.LogIn logIn;
	
	//
	// Navigation
	//
	
	@org.apache.tapestry5.annotations.Component(parameters = {"userExists = userExists"})
	private Navigation navigation;
	
	@org.apache.tapestry5.annotations.Component
	private SharedNavigation sharedNavigationDealer;
	
	@org.apache.tapestry5.annotations.Component
	private SharedNavigation sharedNavigation;
	
	@org.apache.tapestry5.annotations.Component
	private AdminNavigation adminNavigation;
	
	
	//
	// Event handlers
	//
	
	@SetupRender
	public void onSetupRender() {
		tmpUser = new UserBean();
	}
	
	Object onActionFromNavigation() {
		logOutHelper.execute();
		user = null;
		
		return Index.class;
	}
	
	@InjectPage
	private Preview preview;
	
	Object onActionFromSharedNavigation(String s) {
		preview.setPreviewType(s);
		
		return preview;
	}	
	
	Object onActionFromSharedNavigationDealer(String s) {
		preview.setPreviewType(s);
		
		return preview;
	}
	
	//
	//
	//
	
	@Inject
	private ComponentResources resources;
	
	public String getFullPageName() {
		Component page = resources.getContainer();
		return page.getClass().getName();
	}
	
	public String getPageName() {
		String s = "";
		String[] parsed = getFullPageName().split("\\.");
		s = parsed[parsed.length - 1];
		
		return s;
	}
	
	public boolean isIndex() {
		return getFullPageName().equalsIgnoreCase(Index.class.getName());
	}
	
	public boolean isRegistration() {
		return getFullPageName().equalsIgnoreCase(Registration.class.getName());
	}
	
	public boolean isDealer() {
		return getFullPageName().equalsIgnoreCase(Dealer.class.getName());
	}
	
	public boolean isLogIn() {
		return getFullPageName().equalsIgnoreCase(LogIn.class.getName());
	}
	
	public boolean isPrewiew() {
		return getFullPageName().equalsIgnoreCase(Preview.class.getName());
	}
	
	public boolean isPurchase() {
		return getFullPageName().equalsIgnoreCase(Purchase.class.getName());
	}
	
	public boolean isShowCart() {
		return getFullPageName().equalsIgnoreCase(ShowCart.class.getName());
	}
	
	public boolean isShowUsers() {
		return getFullPageName().equalsIgnoreCase(ShowUsers.class.getName());
	}
	
	public boolean isUserDetails() {
		return getFullPageName().equalsIgnoreCase(UserDetails.class.getName());
	}
}
